﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//using Microsoft.WPF.Framework.Graphics;
//using Microsoft.WPF.Framework;
using Gestures;
using GesturePreviews.Gestures;
using System.Windows;
//using GameSpace;

using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
using System.Windows.Media.Effects;

namespace WPFGestures.Gestures.WPF
{
    public class WPFGesture : Gesture
    {        
        protected static Canvas canvas;

        bool gestureCompleted = false;
        bool gestureJustCompleted = false;
        bool toBeDeleted = false;

        Color color;
        public Path path;


        public static void setCanvas(Canvas c)
        {
            canvas = c;
        }

        public static Canvas getCanvas()
        {
            return canvas;
        }

        public WPFGesture(int newID) : base(newID)
        {
            if (canvas == null)
            {
                throw new Exception("Cannot make WPFGesture, Canvas not set.");
            }

            //color = Color.FromRgb(0, 255, 0);
            //Brush b = Brushes.Black.Clone();
            //b.Opacity = 0.5;
            //b.col

            path = new Path();
            path.Stroke = Brushes.Yellow;
            path.Opacity = 0.5;
            path.StrokeThickness = 20;
            SolidColorBrush mySolidColorBrush = new SolidColorBrush();
            mySolidColorBrush.Color = Color.FromArgb(255, 255, 255, 125);
            path.Fill = mySolidColorBrush;

            //DependencyProperty dp = new OpacityProperty();
            //Prop
            //path.
            //An

            canvas.Children.Add(path);
        }

        public override void update()
        {
            if (toBeDeleted)
            {
                return;
            }

            if (!gestureCompleted)
            {
                // decrease alpha values of gesture path over time
                
                //for (int i = 0; i < vertices.Length; ++i)
                //{
                    //vertices[i].Color.A = (byte) (vertices[i].Color.A * 0.8);
                //}
            }

            else if (gestureCompleted)
            {
                if (gestureJustCompleted)
                {
                    gestureJustCompleted = false;
                    // set color to white, alpha to start value
                    //for (int i = 0; i < vertices.Length; ++i)
                    //{
                        //vertices[i].Color = Color.White;
                        //vertices[i].Color.A = 255;
                    //}
                }
                else
                {
                    if (DateTime.Now.Subtract(lastTime).TotalSeconds > 0.5f)
                    {
                        toBeDeleted = true;
                    }
                    // do flashbang animation
                }
            }
        }


        public override void draw()
        {
            GeometryGroup myGeometryGroup = new GeometryGroup();

            PathGeometry pg = new PathGeometry();
            PathFigure pf = new PathFigure();
            pf.StartPoint = new Point(points[0].x, points[0].y);
            pf.IsFilled = false;
            //pf.st
            //pg.

            for (int i = 1; i < points.Count; ++i)
            {

                //points[i].x , points[i].y 

                //float p1x = (float)(points[i].x) * 1000 + position.x;
                //float p1y = position.y - (float)(points[i].y) * 1000;

                LineSegment ls = new LineSegment();//new Point((double)p1x, (double)p1y));
                ls.IsSmoothJoin = true;
                ls.IsStroked = true;

                ls.Point = new Point((double)points[i].x, (double)points[i].y);


                pf.Segments.Add(ls);

                pf.IsClosed = false;
            }

            pg.Figures.Add(pf);
            myGeometryGroup.Children.Add(pg);

            //Console.WriteLine("is = " + imageSource);
            //imageSource = "C:\\Documents and Settings\\cleveringawp\\Desktop\\MtSlideTest\\MtSlideTest\\bin\\Debug\\mirror.png";
            //myGeometryGroup.Transform.
            //path.Opacity = 0;
            path.Data = myGeometryGroup;

            path.Effect = new DropShadowEffect() { Color = Colors.White, ShadowDepth = 0, Opacity = 0.75 };

            /*
            // Add all the geometries to a GeometryGroup.
            GeometryGroup myGeometryGroup = new GeometryGroup();

            for (int i = 0; i < points.Count - 1; ++i)
            {
                LineGeometry line = new LineGeometry();
                line.StartPoint = new Point(points[i].x , points[i].y );
                line.EndPoint = new Point(points[i + 1].x , points[i + 1].y );
                myGeometryGroup.Children.Add(line);
            }
            
            */

            path.Data = myGeometryGroup;

/*
            // Create the ellipse geometry to add to the Path
            EllipseGeometry myEllipseGeometry = new EllipseGeometry();
            myEllipseGeometry.Center = new Point(40, 70);
            myEllipseGeometry.RadiusX = 30;
            myEllipseGeometry.RadiusY = 30;

            // Create a rectangle geometry to add to the Path
            RectangleGeometry myRectGeometry = new RectangleGeometry();
            myRectGeometry.Rect = new Rect(30, 55, 100, 30);


            myGeometryGroup.Children.Add(myLineGeometry);
            myGeometryGroup.Children.Add(myEllipseGeometry);
            myGeometryGroup.Children.Add(myRectGeometry);
            */
            //myPath.Data = myGeometryGroup;

            // Add path shape to the UI.
            //StackPanel mainPanel = new StackPanel();
            //mainPanel.Children.Add(myPath);
            //this.Content = mainPanel;


        }


        public override void  setFingerReleased()
        {
            //gestureCompleted = true;
            //gestureJustCompleted = true;
            canvas.Children.Remove(path);
        }

        public override bool isFingerReleased()
        {
            return gestureCompleted;
        }

        public override bool canBeDeleted()
        {
            return toBeDeleted;
        }
        
        public override void addPoint(Common.Point p)
        {
            base.addPoint(p);

            if (points.Count > 1)
            {
                /*
                VertexPositionColorTexture[] end = createPreviewSegment(points[points.Count - 1], points[points.Count - 2], color);

                int newLength = vertices.Length + 6;
                VertexPositionColorTexture[] newVertices = new VertexPositionColorTexture[newLength];

                Array.Copy(vertices, newVertices, vertices.Length);
                Array.Copy(end, 0, newVertices, vertices.Length, 6);

                vertices = newVertices;
      */
            }            
        }



    }
}
